package com.haierCamera.customapplication.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.haierCamera.customapplication.BaseActivity;
import com.haierCamera.customapplication.Constants;
import com.haierCamera.customapplication.R;
import com.haierCamera.customapplication.databinding.HzklActivitySplashBinding;
import com.haierCamera.customapplication.ui.MainActivity;
import com.haierCamera.customapplication.utils.SharedPreferencesUtils;
import com.haierCamera.customapplication.utils.android.SystemUIHelper;

import org.jetbrains.annotations.NotNull;

public class HZKLSplashActivity extends BaseActivity {
    private HzklActivitySplashBinding binding;
    private int[] mViewList = {R.drawable.hzkl_splash_one, R.drawable.hzkl_splash_two};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SystemUIHelper.setFullWindowNew(getWindow());
        binding = DataBindingUtil.setContentView(this, R.layout.hzkl_activity_splash);
        initData();
    }

    private void initData() {
        binding.next.setOnClickListener(view1 -> {
            SharedPreferencesUtils.saveString(Constants.SPLASH, "yes");
            startActivity(new Intent(HZKLSplashActivity.this, MainActivity.class));
            finish();
        });
        binding.viewPager.setAdapter(new MyPagerAdapter());
        binding.viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 1) {
                    binding.next.setVisibility(View.VISIBLE);
                } else {
                    binding.next.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    class MyPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return mViewList.length;
        }

        @NotNull
        @Override
        public Object instantiateItem(ViewGroup container, int position) {//必须实现，实例化
            View view = View.inflate(HZKLSplashActivity.this, R.layout.hzkl_adapter_viewpage, null);
            ImageView imageView = view.findViewById(R.id.iconView);
            imageView.setImageResource(mViewList[position]);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {//必须实现，销毁
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }
    }

}
